package com.users.rn.rncommon.router;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.users.rn.common.init.ALHReactNativeInit;
import com.users.rn.rncommon.model.SchemeModule;
import com.users.rn.rncommon.ui.RNPluginUpgradeActivity;
import com.users.rn.rncommon.util.RNDebugUtils;
import com.users.rn.rncommon.util.RNLogger;
import com.users.rn.rncommon.util.ReactConst;
import com.users.rn.rncommon.util.ReactUtils;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 15:12
 */
public final class RNSchemeExecute {

    private static String SCHEME_CONTENT_PREFIX = "?content=";

    //打开RN页面的scheme
    private static String RN_SCHEME_OPEN_PAGE = "users://rn_openpage?content=";
    private static final RNSchemeExecute ourInstance = new RNSchemeExecute();

    public static RNSchemeExecute getInstance() {
        return ourInstance;
    }

    private RNSchemeExecute() {
    }

    /**
     * 解析内容
     */
    private String parseContent(String scheme) {
        int index = scheme.indexOf(SCHEME_CONTENT_PREFIX);
        if (index >= 0) {
            return scheme.substring(index + SCHEME_CONTENT_PREFIX.length());
        } else {
            return scheme;
        }
    }

    /**
     * 打开rn页面，自动判断插件是否下载
     *
     * @param context
     * @param url
     */
    public void openPage(Context context, String url) {
        if (context == null || TextUtils.isEmpty(url)) {
            RNLogger.e("打开rn页面失败,context = " + context + " url = " + url);
            return;
        }
        url = parseContent(url);
        RNDebugUtils.RNDebugModel debugModel = RNDebugUtils.deserialize(context, url);
        if (debugModel != null) {
            RNDebugUtils.setDebugStatus(context, debugModel);
        }
        SchemeModule model = SchemeModule.deserialize(url);
        if (model == null) {
            RNLogger.e("打开rn页面失败,解析失败  url = " + url);
            return;
        }
        if (model.isModelValid()) {
            if (ReactUtils.getDebugPluginId(context, model.getPluginid()) || (debugModel != null && debugModel.isEnable)) {//如果是debug模式，直接进入页面
                RNRouterManager.open(context, model);
            } else if (ReactUtils.checkJsBundleFileExistFromAll(context, model.getPluginid())) {
                RNRouterManager.open(context, model);
            } else {
                if (!ALHReactNativeInit.INSTANCE.getInitInterface().isTestEnv()) {
                    RNPluginUpgradeActivity.start(context, model.getPluginid(), url, "RN");
                } else {
                    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
                    boolean rnDownloadFlag = sharedPreferences.getBoolean(ReactConst.RN_PLUGIN_DOWNLOAD, true);
                    if (rnDownloadFlag) {
                        RNPluginUpgradeActivity.start(context, model.getPluginid(), url, "RN");
                    } else {
                        RNLogger.e("IReactNativeSchemeRegister rnDownloadFlag = false");
                    }
                }
            }
        } else {
            RNLogger.e("打开rn页面失败,解析失败  校验失败 url: " + url);
        }
    }
}
